package com.assistant.activity;

import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.assistant.R;
import com.assistant.adapter.ChatMessageAdapter;
import com.assistant.bean.ChatMessage;
import com.assistant.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class TalkActivity extends Activity {
    private ListView mMsgs;
    private ChatMessageAdapter mAdapter;
    private List<ChatMessage> mDatas;

    private EditText mInputMsg;
    private Button mSendMsg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talk);
        initView();

        initDatas();
        initListener();
    }

    private void initView(){

        mMsgs = (ListView) findViewById(R.id.id_listview_msgs);
        mInputMsg = (EditText) findViewById(R.id.id_input_msg);
        mSendMsg = (Button) findViewById(R.id.id_send_msg);
    }

    private void initDatas(){
        mDatas = new ArrayList<ChatMessage>();
        mDatas.add(new ChatMessage("你好，小狐狸为您服务", ChatMessage.Type.INCOMING,new Date()));
        mAdapter = new ChatMessageAdapter(this,mDatas);
        mMsgs.setAdapter(mAdapter);
    }

    private void initListener(){
        mSendMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String toMsg = mInputMsg.getText().toString();
                if(TextUtils.isEmpty(toMsg)){
                    Toast.makeText(TalkActivity.this,"啥都不写发你妹啊！",Toast.LENGTH_SHORT).show();
                    return;
                }

                ChatMessage toMessage = new ChatMessage();
                toMessage.setDate(new Date());
                toMessage.setMsg(toMsg);
                toMessage.setType(ChatMessage.Type.OUTCOMING);
                mDatas.add(toMessage);
                mAdapter.notifyDataSetChanged();
                mMsgs.setSelection(mDatas.size() - 1);
                mInputMsg.setText("");
                new Thread(){
                    public void run(){
                        ChatMessage fromMessage = HttpUtils.sendMessage(toMsg);
                        Message m = Message.obtain();
                        m.obj = fromMessage;
                        mHandler.sendMessage(m);
                    }
                }.start();
            }
        });
    }

    private static ChatMessage sendMsg(String msg){
        String url = "http://www.tuling123.com/openapi/api";
        Map<String,String> param = new HashMap<>();
        param.put("key","aca837fd8231e03318b928c7d74f41ff");
        param.put("info",msg);
        try {
            String jsonStr = HttpUtils.get(url,param);
            JSONTokener jsonParser = new JSONTokener(jsonStr);
            JSONObject json = (JSONObject) jsonParser.nextValue();
            String code = json.getString("code");
            String text = json.getString("text");
            ChatMessage cm = new ChatMessage();
            cm.setDate(new Date());
            cm.setType(ChatMessage.Type.INCOMING);
            if("100000".equalsIgnoreCase(code)){
                cm.setMsg(text);
            }else{
                cm.setMsg("完全不知道你在说什么！！！");
            }
            return cm;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Handler mHandler = new Handler(){
        public void handleMessage(android.os.Message msg){
           ChatMessage fromMessage = (ChatMessage)  msg.obj;
            mDatas.add(fromMessage);
            mAdapter.notifyDataSetChanged();
            mMsgs.setSelection(mDatas.size()-1);
        }
    };

}
